package com.haois.mp.database.module.wxserver.util;

import com.haois.mp.database.module.common.daomain.Driver;
import com.haois.mp.database.module.common.daomain.Vehicle;
import com.haois.mp.database.util.jx.DateUtil;
import java.util.Calendar;
import java.util.Date;

/**
 * description
 *
 * @author RuiHaoZ on Hgits.cn
 * @date 2018/9/5 13:58
 */
public class VehicleUtil {


    public static int daysBetween(String regTimeStr) {
        Date regTime = DateUtil.stringToDate(regTimeStr, DateUtil.yMd);
        Date regThisYear = setThisYear(regTime);
        Date thisTime = new Date();
        int days = DateUtil.daysBetween(thisTime, regThisYear);
        return days;
    }

    public static int getDriverValidPeriodDays(String regTimeStr) {
        int days = daysBetween(regTimeStr);
        days = days < 0 ? days + 365 : days;
        return days;
    }

    public static Date getDriverValidPeriodTime(String regTimeStr) {
        int days = daysBetween(regTimeStr);
        Date regTime = DateUtil.stringToDate(regTimeStr, DateUtil.yMd);
        regTime = days > 0 ? setThisYear(regTime) : setLastYear(regTime);
        return regTime;
    }

    public static Date setThisYear(Date date) {
        Calendar thisCalender = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, thisCalender.get(Calendar.YEAR));
        return calendar.getTime();
    }

    public static Date setLastYear(Date date) {
        Calendar thisCalender = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, thisCalender.get(Calendar.YEAR) + 1);
        return calendar.getTime();
    }

    public static StringBuffer getVehicleInfo(Vehicle vehicle) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("您所关联的车辆共存在").append(vehicle.getWzSize()).append("条违章未处理，")
                .append("合计").append(vehicle.getWzFen()).append("分").append(vehicle.getWzMoney()).append("元")
                .append("\n");

        return stringBuffer;
    }

    public static StringBuffer getVehicleInfo(Vehicle vehicle, Date nearTime, int days) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("您所关联的车辆【").append(vehicle.getPlate()).append("】需于")
                .append(DateUtil.getTime(DateUtil.yMd_HZ, nearTime)).append("前年检，")
                .append("还剩").append(days).append("天。")
                .append("存在").append(vehicle.getWzSize()).append("条违章未处理，")
                .append("共计").append(vehicle.getWzFen()).append("分").append(vehicle.getWzMoney()).append("元")
                .append("\n")
                .append("数据统计截止日期：").append(vehicle.getWzUpdateTime())
                .append("\n");
        return stringBuffer;
    }

    public static StringBuffer getDriverInfo(Driver driver) {
        int days = VehicleUtil.getDriverValidPeriodDays(driver.getRegister());
        String periodTime = DateUtil.getTime(DateUtil.yMd_HZ,
                VehicleUtil.getDriverValidPeriodTime(driver.getRegister()));
        driver.setClearTime(periodTime);
        driver.setClearDays(days);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("您所关联的驾驶证【").append(driver.getName()).append("】将于")
                .append(driver.getClearTime()).append("分数清零，")
                .append("还剩").append(driver.getClearDays()).append("天，")
                .append("剩余").append(12 - driver.getScore()).append("分")
                .append("\n")
                .append("数据统计截止日期：").append(driver.getWzUpdateTime())
                .append("\n");
        return stringBuffer;
    }


    /*Annual Inspection and examination of Vehicles*/
    //在6年以内是以两年一次年检的频率来算的，超过了6年未满15年，就是按照一年一次年检，而在15年以上，车辆就需要每半年进行一年年检了
    public static Date vehicleInspection(String regTime) {

        Date regDate = DateUtil.stringToDate(regTime, DateUtil.yMd);
        Date nearTime = setThisYear(regDate);
        Calendar regCal = Calendar.getInstance();

        regCal.setTime(regDate);
        int regYear = regCal.get(Calendar.YEAR);

        Calendar thisCal = Calendar.getInstance();
        Date thisDate = thisCal.getTime();

        int thisYear = thisCal.get(Calendar.YEAR);
        int velAge = thisYear - regYear;

   /*     int extendMonth = 12;
        for (int k = 0; k < 6; k = k + 2) {
            if (regDate.after(thisDate)) {
                break;
            }
            regDate = addMonth(regDate, extendMonth * 2);

        }
        for (int k = 6; k < 15; k = k + 1) {
            if (regDate.after(thisDate)) {
                break;
            }
            regDate = addMonth(regDate, extendMonth * 1);

        }
        for (int k = 0; ; k = k + 1) {
            if (regDate.after(thisDate)) {
                break;
            }
            regDate = addMonth(regDate, extendMonth / 6);

        }
        return regDate;*/
        int extendYear = 0;
        if (velAge < 6) {
            extendYear = velAge % 2 == 1 ? velAge + 1 : velAge;
            nearTime = addMonth(regDate, extendYear * 12);
            if (nearTime.before(thisDate)) {
                nearTime = addMonth(nearTime, 24);
            }
        } else if (velAge < 15) {
            extendYear = velAge;
            nearTime = addMonth(regDate, extendYear * 12);
            if (nearTime.before(thisDate)) {
                nearTime = addMonth(nearTime, 12);
            }
        } else if (velAge > 15) {
            nearTime = addMonth(regDate, 12 * velAge);
            if (nearTime.before(thisDate)) {
                nearTime = addMonth(nearTime, 6);
            }
            if (nearTime.before(thisDate)) {
                nearTime = addMonth(nearTime, 6);
            }
        }
        return nearTime;
    }

    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, month + calendar.get(Calendar.MONTH));
        DateUtil.getYMd(calendar.getTime());
        return calendar.getTime();
    }

    public static void main(String[] args) {


    }
}
